<template>
  <!-- 登录页面 -->
  <div>
    <Row class="container">
      <i-col span="13" offset="2" class="login-img-box">
        <img src="../assets/sale.jpg" alt />
      </i-col>
      <i-col span="7" class="login-box">
        <div class="login-container">
          <div class="login-header">
            <p>欢迎登录</p>
          </div>
          <div class="form-box">
            <Form ref="formInline" :model="loginForm" :rules="ruleInline">
              <FormItem prop="username">
                <i-input
                  type="text"
                  v-model="loginForm.username"
                  clearable
                  size="large"
                  placeholder="用户名"
                >
                  <Icon type="ios-person" slot="prepend"></Icon>
                  
                </i-input>
              </FormItem>
              <FormItem prop="password">
                <i-input
                  type="password"
                  v-model="loginForm.password"
                  clearable
                  size="large"
                  placeholder="密码"
                >
                  <Icon type="ios-lock" slot="prepend"></Icon>
                </i-input>
              </FormItem>
              <FormItem>
                <Button type="error" size="small" @click="Login" long>登录</Button>
              </FormItem>
            </Form>
          </div>
        </div>
      </i-col>
    </Row>
  </div>
</template>

<script>
import New from "@/new.js";
import { mapState,mapMutations } from "vuex";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: 'user',
        password: '123456'
      },
      userid:'',
      msg: "",
      ruleInline: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            type: "string",
            min: 6,
            message: "密码不能少于6位",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created:{
      // this.$store.commit('changeID',3)
  },
  methods: {
     handle(){
            // this.$store.commit('add',3)
        },
    Login() {
       console.log("登录成功");
      this.$axios
        .post("userLogin", {
          params: this.loginForm
        })
        .then(result => {
          // 用户id
          console.log(result.data.data);
          this.msg = result.data.msg;
          if (result.data.status !== 200) return this.$message.error(this.msg);
          window.sessionStorage.setItem("token", result.data.token);
          this.$message.success(this.msg);
          /*  1.将登陆成功之后的token,保存到客户端的sessionStorage
                  1.1 项目中除了登陆之外的其他API接口，必须在登陆之后才能访问
                  1.2 token保存在sessionStorage */
          window.sessionStorage.setItem("token", result.data.token);
          window.sessionStorage.setItem("userid",result.data.data[0].userid)
          window.sessionStorage.setItem("username",result.data.data[0].username)

          console.log(window.sessionStorage.token);
          // 把拿到的userid传给mutations
          console.log(result.data.data[0].userid)
          this.$store.commit('add',result.data.data[0].userid)
          // this.$store.commit('add',result.data.data[0].recename)

          this.$store.commit('changename',result.data.data[0].username)

          this.$store.commit('save',result.data.data)
          // 兄弟组件传值
          New.$emit("getisshow", {
            show: this.loginForm.username
          });
          New.$emit("token", {
            show: result.data.token
          });
          New.$emit("userid", {
            show: this.userid
          });
        // 验证通过后跳到home
          this.$router.push({ path: "home" });
          // location.reload();
          this.$router.go(0);


        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style scoped>
.container {
  margin-top: 30px;
  height: 555px;
  background-color: #fff;
}
.login-img-box {
  height: 485px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-img-box > img {
  width: 68%;
}
.login-box {
  height: 485px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-container {
  width: 80%;
  height: 280px;
  border: #ed3f14 solid 1px;
}
.login-header {
  height: 50px;
  font-size: 20px;
  text-align: center;
  line-height: 50px;
  letter-spacing: 5px;
  color: #fff;
  background-color: #ed3f14;
}
.form-box {
  width: 80%;
  margin: 30px auto;
}
</style>
